#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os
from autobahn.twisted import choosereactor
choosereactor.install_optimal_reactor(False)
sys.path.insert(0, os.path.split(__file__)[0])
from toughportal.tools import config as iconfig
from twisted.protocols.ftp import FTPFactory, FTPRealm
from twisted.cred.portal import Portal
from twisted.cred.checkers import FilePasswordDB
from twisted.internet import reactor


def run(config):
    print 'running portal ftp server...'
    ftpdir = os.path.abspath(os.path.join(os.path.dirname(__file__), "toughportal"))
    print "ftp dir", ftpdir
    p = Portal(FTPRealm(ftpdir, userHome=ftpdir), [FilePasswordDB("/etc/pftp.conf")])
    f = FTPFactory(p)
    try:
        ftpport = config.getint("admin", "ftpport")
    except:
        ftpport = 10021
    reactor.listenTCP(ftpport, f)
    reactor.run()


if __name__ == '__main__':
    run(iconfig.find_config())
    reactor.run()
